package org.qiancizhan.UI;

import org.qiancizhan.API.ALiYun_TranslateGeneral;
import org.qiancizhan.Controller.LanguageController;
import org.qiancizhan.Model.DTO.TranslateDTO;
import org.qiancizhan.Model.POJO.LanguagePOJO;
import org.qiancizhan.Model.VO.TranslateVO;
import org.qiancizhan.Utils.UIFormatUtli;

import java.util.List;
import java.util.Scanner;

public class TranslateUI {
    private static final Scanner sc = new Scanner(System.in);
    private static String targetLanguage = "en";
    private static final LanguageController languageController = new LanguageController();
    public static void showTranslateMenu() {

        UIFormatUtli.log("本翻译工具采用的是ALiYun服务机器翻译");
        System.out.println("欢迎使用在线翻译，默认输出语言为英文");
        System.out.println("1.切换目标语言\t2.查看支持的语言\t0.退出翻译");
        label:
        while (true) {
            System.out.println("请输入文本");
            String input = sc.nextLine().trim();
            switch (input) {
                case "0":
                    System.out.println("退出翻译");
                    break label;
                case "1":
                    while (true) {
                        System.out.println("请输入目标国家语言(例如日语)\t0.退出");
                        input = sc.nextLine().trim();
                        if (input.equals("0")) break;
                        LanguagePOJO languagePOJO = languageController.getLanguageByLang(input);
                        if(languagePOJO == null) {
                            UIFormatUtli.log("未找到该语言");
                            continue;
                        }
                        targetLanguage = languagePOJO.getLangCode();
                        UIFormatUtli.log(String.format("已切换目标语言为：%s", languagePOJO.getLang()));
                    }
                    break;
                case "2":
                    List<LanguagePOJO> languages = languageController.getAllLanguage();
                    languages.forEach(i -> {
                        UIFormatUtli.log(String.format("目标语言：%s\t目标代码：%s", i.getLang(), i.getLangCode()));
                    });
                    break;
                default:
                    try {
                        TranslateVO res = ALiYun_TranslateGeneral.translate(new TranslateDTO(input, "auto", targetLanguage));
                        UIFormatUtli.log("翻译结果:");
                        System.out.println();
                        System.out.println(res.getTranslated());
                        System.out.println();
                        System.out.println(UIFormatUtli.HR);
                    } catch (Exception e) {
                        System.out.println("翻译失败");
                    }
                    break;
            }
        }
    }
}
